import { PageConfig } from 'next';
import anylogger from 'anylogger';
import '@mmstudio/an000042';
import an48 from '@mmstudio/an000048';
import an8 from '@mmstudio/an000008';
import { tbstudent } from '../../../../../atoms/db';

const logger = anylogger('pages/api/registration/registration1/save/api.api');

export type Result = {
	ok: true;
} | {
	ok: false;
	message: string;
};

export type Message = {
	rejectionreasons: string;
	approvalstatus: string;
};


/**
 * 保存
 */
const handler = an48<Result>();

handler.post(async (req, res) => {
	try {
		logger.debug('msg body:', req.body);
		const body = req.body as Message;
		const { approvalstatus, rejectionreasons } = req.body as Message;

		if (!body
			|| !body.rejectionreasons
			|| !body.approvalstatus
		) {
			res.status(200).json({
				ok: false,
				message: '缺少必要的参数'
			});
			return;
		}
		await tbstudent().insert({
			...body,
			approvalstatus: approvalstatus,
			rejectionreasons: rejectionreasons
		});
		res.status(200).json({ ok: true });
	} catch (error) {
		logger.trace(error);
		res.status(200).json({ ok: false, message: (error as Error).message });
	}
});
export const config = {} as PageConfig;

export default handler;
